package com.lanhe.admin.controller.article;


import com.lanhe.article.domain.LhArticle;
import com.lanhe.article.service.ILhArticleService;
import com.lanhe.common.annotation.Log;
import com.lanhe.common.core.controller.BaseController;
import com.lanhe.common.core.domain.AjaxResult;
import com.lanhe.common.core.page.TableDataInfo;
import com.lanhe.common.enums.BusinessType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 文章表(LhArticle)表控制层
 *
 * @author zhanggl
 * @since 2023-12-18 09:23:41
 */
@RestController
@RequestMapping("lhArticle")
public class    LhArticleController  extends BaseController {

    /**
     * 服务对象
     */
    @Resource
    private ILhArticleService lhArticleService;

    /**
     * 分页查询所有数据
     */
    @PreAuthorize("@ss.hasPermi('article:lhArticle:list')")
    @GetMapping("/list")
    public TableDataInfo list(LhArticle article) {
        startPage();
        List<LhArticle> list = lhArticleService.selectArticleList(article);
        return getDataTable(list);
    }

    /**
     * 根据编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('article:lhArticle:query')")
    @GetMapping(value = {"/", "/{articleId}"})
    public AjaxResult getInfo(@PathVariable(value = "articleId") Long articleId) {
        return success(lhArticleService.selectArticleById(articleId));
    }

    /**
     * 新增文章
     */
    @PreAuthorize("@ss.hasPermi('article:lhArticle:add')")
    @Transactional
    @Log(title = "文章管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody LhArticle article) {

        article.setCreateBy(getUsername());
        article.setCreateTime(new Date());
        lhArticleService.insertArticle(article);
        return success();
    }

    /**
     * 修改新增文章
     */
    @PreAuthorize("@ss.hasPermi('article:lhArticle:edit')")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody LhArticle article) {
        if (ObjectUtils.isEmpty(article.getArticleId())
                || null == article.getArticleId()) {
            return error("主键ID不能为空");
        }
        article.setUpdateBy(getUsername());
        article.setUpdateTime(new Date());
        return toAjax(lhArticleService.updateArticle(article));
    }

    /**
     * 删除新增文章
     */
    @PreAuthorize("@ss.hasPermi('article:lhArticle:remove')")
    @Log(title = "文章管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{articleIds}")
    public AjaxResult remove(@PathVariable Long[] articleIds)
    {
        if (ObjectUtils.isEmpty(articleIds)){
            return error("主键ID不能为空");
        }
        return toAjax(lhArticleService.deleteArticleByIds(articleIds));
    }

}

